from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QMovie
from config import LOADING_GIF_PATH

class WaitingDialog(QDialog):
    """等待加载窗口"""
    def __init__(self, message="正在处理，请稍候...", parent=None):
        super().__init__(parent)
        self.setWindowTitle("处理中")
        self.setFixedSize(250, 150)
        self.setWindowFlags(Qt.Window | Qt.WindowTitleHint | Qt.CustomizeWindowHint)
        self.setModal(True)  # 模态窗口，阻塞父界面
        self.init_ui(message)

    def init_ui(self, message):
        """初始化界面"""
        main_layout = QVBoxLayout(self)
        main_layout.setAlignment(Qt.AlignCenter)
        main_layout.setContentsMargins(20, 20, 20, 20)

        # 提示文字
        self.loading_label = QLabel(message)
        self.loading_label.setAlignment(Qt.AlignCenter)
        self.loading_label.setStyleSheet("color: white; font-size: 16px; font-weight: bold;")
        main_layout.addWidget(self.loading_label)

        # GIF/文字加载动画
        self.gif_label = QLabel()
        self.gif_label.setAlignment(Qt.AlignCenter)
        self.movie = QMovie(LOADING_GIF_PATH)
        if self.movie.isValid():
            self.movie.setScaledSize(QSize(64, 64))
            self.gif_label.setMovie(self.movie)
            self.movie.start()
        else:
            print(f"警告：未找到加载动画文件 {LOADING_GIF_PATH}，使用文字替代")
            self.gif_label.setText("加载中...")
            self.gif_label.setStyleSheet("color: #ADD8E6; font-size: 14px;")
        main_layout.addWidget(self.gif_label)

        # 样式表
        self.setStyleSheet("""
            WaitingDialog {
                background-color: #3C3F41;
                border: 2px solid #606060;
                border-radius: 8px;
            }
        """)